//
//  SharedView.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/3.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "SharedView.h"


@interface SharedView()

@property (nonatomic, strong) UIView *backgroudView;
/** <#注释说明#> */
@property (nonatomic, strong) UIButton *clearBtn;
/** <#注释说明#> */
@property (nonatomic, strong) UIView *buttonView;
/** <#注释说明#> */
@property (nonatomic, strong) NSArray *buttonArray;
/** <#注释说明#> */
@property (nonatomic, copy) SharedEvent shardEvent;

@end

@implementation SharedView

- (instancetype)sharedViewFrame:(CGRect)frame sharedBtnComplent:(SharedEvent)complent{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _shardEvent = complent;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews{
    
    _buttonArray = @[@"微信",@"朋友圈",@"微博",@"QQ",@"QQ空间",@"二维码",@"复制链接"];
    NSArray *imageArray = @[@"weixin",@"pengyouquan",@"weibo",@"QQ",@"QQkongjian",@"saoyisao",@"fuzhilianjie"];
    self.backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHEIGH, ScreenWIDTH, ScreenHEIGH/2)];
    self.backgroudView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.backgroudView];
    
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, ScreenWIDTH-20, (ScreenHEIGH/2)-90)];
    self.buttonView.backgroundColor = [UIColor whiteColor];
    self.buttonView.layer.cornerRadius = 5;
    self.buttonView.layer.masksToBounds = YES;
    [self.backgroudView addSubview:self.buttonView];
    
    CGFloat width = (ScreenWIDTH-70)/4;
    CGFloat height = width;
    for (int i = 0; i < _buttonArray.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10+(width+10)*(i%4), 25+(height+25)*(i/4), width, height)];
        button.tag = i;
        [button setTitle:_buttonArray[i] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonViewEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(button.frame.size.height+25, 0, 0, 0)];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.buttonView addSubview:button];
    }
    
    self.clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.clearBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.clearBtn.backgroundColor = [UIColor whiteColor];
    [self.clearBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.clearBtn.frame = CGRectMake(10, self.backgroudView.frame.size.height-70, ScreenWIDTH-20, 50);
    [self.clearBtn addTarget:self action:@selector(clearBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    self.clearBtn.layer.cornerRadius = 5;
    self.clearBtn.layer.masksToBounds = YES;
    [self.backgroudView addSubview:self.clearBtn];
    
}

- (void)showShardView{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window) {
        window = [UIApplication sharedApplication].windows[0];
    }
    [window addSubview:self];
    [self showShardViewAnimate];
}

- (void)showShardViewAnimate{
   
    CTL_WS
    [UIView animateWithDuration:0.25 animations:^{
        ws.backgroudView.frame = CGRectMake(0, ScreenHEIGH - ScreenHEIGH/2, ScreenWIDTH, ScreenHEIGH/2);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissShardAnimate{
    CTL_WS
    [UIView animateWithDuration:0.25 animations:^{
        ws.backgroudView.frame = CGRectMake(0, ScreenHEIGH, ScreenWIDTH, ScreenHEIGH/2);
    } completion:^(BOOL finished) {
        [ws removeFromSuperview];
    }];
}

- (void)clearBtnEvent{
    [self dismissShardAnimate];
}

- (void)buttonViewEvent:(UIButton *)button{
    [self dismissShardAnimate];
    if (_shardEvent) {
        _shardEvent(_buttonArray[button.tag]);
    }
}

@end
